<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <base th:href="@{/}">
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/static/layui/css/layui.css}">
    <link rel="stylesheet" th:href="@{/static/css/common.css}">
</head>
<body>
<form lay-filter="scenicForm" class="layui-form model-form">

    <div class="layui-form-item">
        <label class="layui-form-label">轮播图</label>
        <div class="layui-upload layui-input-block">
            <button type="button" class="layui-btn layui-btn-primary" id="showImgUpload"> <i class="layui-icon">&#xe67c;</i>上传图片</button>
            <img id="simpleImg" width="60px" height="60px">
        </div>
    </div>
    <div class="layui-form-item text-right">
        <button class="layui-btn layui-btn-primary" type="button" id="closeDialog">取消</button>
        <button class="layui-btn" lay-filter="articleFormSubmit" lay-submit>保存</button>
    </div>
</form>
</body>
<script type="text/javascript" th:src="@{/static/layui/layui.js}"></script>
<script type="text/javascript" th:src="@{/static/js/common.js}"></script>
<script type="text/javascript" th:src="@{/static/js/jquery.min.js}"></script>
<script type="text/javascript" th:src="@{/static/js/admin.js}"></script>
<script>
    layui.use(['form','upload','layer','admin'], function() {
        var form = layui.form,
            $=layui.jquery,
            upload=layui.upload,
            layer=layui.layer,
            admin=layui.admin;

        $('#closeDialog').click(function () {
            var index = parent.layer.getFrameIndex(window.name);
            //关闭当前frame
            parent.layer.close(index);
        });
        // 表单提交事件
        form.on('submit(articleFormSubmit)', function (d) {
            d.field.path = $("#simpleImg")[0].src;
            layer.load(2);
            admin.req('picture/insert', d.field, function (res) {
                layer.closeAll('loading');
                layer.alert(res.msg, {icon: res.code == 200 ? 6 : 5},function () {
                    // 获得frame索引
                    var index = parent.layer.getFrameIndex(window.name);
                    //关闭当前frame
                    parent.layer.close(index);
                    //刷新页面
                    parent.location.replace(parent.location.href);
                });
            },'POST');
            return false;
        });

        upload.render({
            elem:"#showImgUpload",
            url:"/sys/imgUpload",
            method:'post',
            accept:'images',
            exts:'jpg|png|gif|bmp|jpeg',
            done: function(res, index, upload){
                if(res.code == 0){
                    layer.msg("文件加载成功！",{icon:1});
                    $("#simpleImg").attr("src",res.image);
                    $("#showImgUpload").addClass("layui-btn-disabled");
                    $("#showImgUpload").off("click");
                }
            }
        });
    });
</script>
</html>